package sair.player.ui;

import java.awt.Color;

import sair.FCM;
import sair.player.PlayerActivity;
import sair.sys.gui.ConsFrame;
import sair.sys.gui.swing.control.SBorder;

public class FlushJSBThread implements Runnable {
	private boolean iscon = true;
	private boolean isInitClick = false;

	private PlayerActivity paction;
	private Color localBarColor;
	private Color localBorderColor;

	public FlushJSBThread(PlayerActivity pa) {
		this.paction = pa;
	}

	public void run() {
		paction.ui.getJSB().setMaximum(paction.getPA().getMaxSize());
		while (this.iscon) {
			if (paction != null && paction.getPA() != null) {
				int now;
				try {
					now = paction.getPA().getNowLimit();
				} catch (Exception e) {
					now = 0;
				}
				paction.ui.getJSB().setValue(now);
				waits();
				paction.ui.getJSB().repaint();
				if (localBarColor != ConsFrame.getFontColor() || localBorderColor != FCM.EXECTION_help_Color)
					reinitColor();

				if (!isInitClick) {
					paction.ui.getJSB().addMouseListener(new BarClick(paction.ui.getJSB(), paction.getPA()));
					isInitClick = true;
				}
			}
			waits();
		}
		paction.ui.getJSB().setMaximum(1);
		paction.ui.getJSB().setValue(0);
	}

	private static void waits() {
		try {
			Thread.sleep(80L);
		} catch (InterruptedException e) {

		}
	}

	private void reinitColor() {
		localBarColor = ConsFrame.getFontColor();
		localBorderColor = FCM.EXECTION_help_Color;
		paction.ui.getJSB().setForeground(localBarColor);
		paction.ui.getJSB().setBorder(new SBorder(localBorderColor));
	}

	public boolean Stop() {
		this.iscon = false;
		return !this.iscon;
	}

}
